package com.ay.selfservicemachine.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;

import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

public class HQWWeightUtil {


    public static int dptopx(Context context, float dpValue) {//DP转换为PX
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int pxtodp(Context context, float pxValue) {//PX转换为DP
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    public static void hideStatusBarText(Activity activity) {//隐藏状态栏文字
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//完全沉浸式
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            activity.getWindow().getDecorView().setSystemUiVisibility(
//                      View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//        }
//        activity.getWindow().getDecorView().setSystemUiVisibility(
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                        | View.SYSTEM_UI_FLAG_FULLSCREEN
//                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

    public static void showSystemStatusBar(Activity activity) {//显示顶部状态栏
        Window window = activity.getWindow();
        WindowCompat.getInsetsController(window, window.getDecorView()).show(WindowInsetsCompat.Type.statusBars());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
            window.setAttributes(lp);
        }
//        WindowCompat.setDecorFitsSystemWindows(window, true); //false 表示沉浸
    }

    public static void hideSystemStatusBar(Activity activity) {//隐藏顶部状态栏
        Window window = activity.getWindow();
//        WindowCompat.getInsetsController(window,window.getDecorView()).hide(WindowInsetsCompat.Type.statusBars());
//
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            window.setAttributes(lp);
        }


        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = window.getDecorView();
            decorView.setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                            View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );
        }

//        WindowCompat.setDecorFitsSystemWindows(window, false); //false 表示沉浸
    }

    public static void showSystemNavigationBar(Activity activity) {//显示底部导航栏
        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT <= 27) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility(0);
        } else {
            WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView()).show(WindowInsetsCompat.Type.navigationBars());
        }
    }

    public static void hideSystemNavigationBar(Activity activity) {//隐藏底部导航栏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View view = activity.getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT <= 27) {//小于等于android9的时候
            View decorView = activity.getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;//隐藏导航栏和稳定
            decorView.setSystemUiVisibility(uiOptions);
        } else {//大于android9的时候
            WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView()).hide(WindowInsetsCompat.Type.navigationBars());
        }
    }

    public static int getPhoneStatusBarHeight(Context context) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return context.getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    public static int getNavigationBarHeight(Context context) {
        int navigationBarHeight = 0;
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            navigationBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }

        return navigationBarHeight;
    }




    public static int getPhoneWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getPhoneHeigh(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }


    public static void drawMonitor(View view, DrawCallback drawCallback) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                drawCallback.onSuccess();
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    public interface DrawCallback {
        void onSuccess();
    }


}
